package org.jradweb.core.orm.validator;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.hibernate.validator.Validator;
import org.jradweb.core.orm.HibernateModel;

public class UniqueValidator implements BeanValidator<Unique>, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2190096482648966783L;

	private String[] propertyName;

	public String[] getPropertyName() {
		return propertyName;
	}

	public void initialize(Unique parameters) {
		this.propertyName = parameters.propertyName();
	}

	public boolean isValid(Object value) {
		if (value == null) {
			return true;
		}

		HibernateModel model = (HibernateModel) value;
		BeanUtilsBean bean = BeanUtilsBean.getInstance();
		Object propertyValue;

		return model.isUnique(model, propertyName);

	}

}
